package com.cloudpivot.receivales.ding.robot.controller;

import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.ding.robot.domain.ErnieMessageDTO;
import com.cloudpivot.receivales.ding.robot.service.baidu.BaiduERNIEServiceImpl;
import com.cloudpivot.receivales.ding.robot.service.baidu.BaiduNPLServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Objects;

/**
 *  文心一言 对外服务接口
 */
@Controller
@Slf4j
@RequestMapping("/baiduService/")
public class BaiduERNIEController {

    @Autowired
    private BaiduERNIEServiceImpl baiduERNIEService;

    /**
     * 调用文心一言4.0 接口服务
     * @param dto
     * @return
     */
    @ResponseBody
    @PostMapping("/ernieApi")
    public JSONObject ernieApi(@RequestBody List<ErnieMessageDTO> dto) {
        if (Objects.isNull(dto)) {
            return resJson(-1, "request is null", null);
        }
        JSONObject ret=baiduERNIEService.ernie4PreView(dto);
        if(StringUtils.isNotBlank(ret.getString("result"))){
            return resJson(0, "success", ret.getString("result"));
        }
        return resJson(-1, "request from baidu error", ret);
    }

    private JSONObject resJson(int code,String msg,Object result){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);
        jsonObject.put("msg", msg);
        jsonObject.put("result", result);
        return jsonObject;
    }
}
